/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import GUI.SQL.TingkatPenjualan;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import koneksi.SQL.Koneksi;

/**
 *
 * @author rosi
 */
public class Penjualan {

    public void getMinMax() {
        int min = 0, max = 0;
        int[] data = {3, 6, 2, 6, 45, 7, 54, 5, 32, 97, 4, 75, 27, 4};

        for (int i = 0; i < data.length; i++) {
            if (i == 0) {
                min = data[i];
                max = data[i];
            } else if (max < data[i]) {
                max = data[i];
            } else if (min > data[i]) {
                min = data[i];
            }
        }
        System.out.println("max = " + max);
        System.out.println("min = " + min);
    }

    public void getData() {
        double jumlah = 0;
        double jml;
        String jumlahStr;
        
        double jmlOrder=0;
        double jumlahOrder=0;
        String orderStr;
        
        String namaTabel = "0001jan";
        int total=0;
        String namaMax="";
        String noMax="";
        String costMax="";

        
        for (int i = 1; i <= 9; i++) {
            double min = 0;
            double max = 0;
            try {
                Koneksi konSQL = new Koneksi();
                Connection conn = konSQL.open();
                Statement st = conn.createStatement();

                String query = " select * from " + namaTabel + " where tipe = '" + i + "'";
                ResultSet rs = st.executeQuery(query);

                while (rs.next()) {
                    jml = Double.parseDouble(rs.getString("jumlah"));
                    jmlOrder = Double.parseDouble(rs.getString("order_qty"));
                    
                    //cek jumlah terbanyak
                    if (total == 0) {
                        min = jml;
                        max = jml;
                    } else if (max < jml) {
                        max = jml;
                        namaMax = rs.getString("prod_desc");
                        noMax=rs.getString("no_barang");
                        costMax=rs.getString("customer");
                    } else if (min > jml) {
                        min = jml;
                    }

                    //hitung jumlah order
                    jumlahOrder = jumlahOrder+jmlOrder;
                    
                    //hitung jumlah kirim
                    jumlah = jumlah + jml;
//                    jumlahStr = String.valueOf(jumlah);
                    total++;
                }
            } catch (SQLException ex) {
                Logger.getLogger(TingkatPenjualan.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("nama max "+i+" : "+namaMax);
            System.out.println("No barang "+i+" : "+noMax);
            System.out.println("Costumer "+i+" : "+costMax);
            System.out.println("Jumlah Kirim "+i+" : "+jumlah);
            System.out.println("Jumlah Order "+i+" : "+jumlahOrder);
            System.out.println("\n");

        }

    }
}
